package org.javaboy.fmall.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.javaboy.fmall.entity.FmBase;
import org.javaboy.fmall.service.FmBaseService;
import org.javaboy.fmall.utils.returnTool.ResTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @ClassName hellocontroller
 * @Description
 * @Author find me
 * @Date 2020/7/16 0016 0:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/fmAll")
@Api(description = "FM_BASE表相关操作")
public class FmBaseController {

    @Autowired
    FmBaseService fmBaseService;


    @ApiOperation(value = "查询fmBase全部数据", notes = "查询fmBase全部数据")
    @GetMapping("/getFmBaseAll")
    public ResTool getFmBaseAll(FmBase fmBase) {
        List<FmBase> fmBases = fmBaseService.getFmBaseAll(fmBase);

        return ResTool.build().data(fmBases);
    }


    @ApiOperation(value = "向fmBase中插入数据", notes = "向fmBase中插入数据")
    @GetMapping("/insertFmBaseDataOne")
    public ResTool insertFmBaseDataOne(FmBase fmBase) {
        Boolean fmBaseDataOne = fmBaseService.insertFmBaseDataOne(fmBase);
        return ResTool.build().data(fmBaseDataOne);
    }

    @ApiOperation(value = "删除fmBase中的数据", notes = "删除fmBase中的数据")
    @GetMapping("/delFmBaseData")
    public ResTool delFmBaseData(FmBase fmBase) {
        Boolean fmBaseDataOne = fmBaseService.delFmBaseData(fmBase);
        return ResTool.build().data(fmBaseDataOne);
    }


    @ApiOperation(value = "更新fmBase中的数据", notes = "更新fmBase中的数据")
    @GetMapping("/updateFmBaseData")
    public ResTool updateFmBaseData(FmBase fmBase) {
        Boolean fmBaseDataOne = fmBaseService.updateFmBaseData(fmBase);
        return ResTool.build().data(fmBaseDataOne);
    }



    /*=======================================================*/
    @ApiOperation(value = "通用表单查询方法 (完成)", notes = "通用表单查询方法")
    @GetMapping("/getFmBaseToDefineId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fromDefineId",value = "表单定义ID", required = true),
            @ApiImplicitParam(name = "pageNo", value = "页码", defaultValue = "1", paramType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", defaultValue = "15", paramType = "int"),
            @ApiImplicitParam(name = "condition", value = "查询条件"),
            @ApiImplicitParam(name = "orderBy", value = "排序条件"),
            @ApiImplicitParam(name = "flag", value = "是否导出Excel", dataType = "boolean"),
            @ApiImplicitParam(name = "paramJson", value = "动态SQL匹配参数"),
            @ApiImplicitParam(name = "queryValue", value = "模糊查询条件值")
    })
    public ResTool getFmBaseToDefineId(String  fromDefineId, Integer pageNo,Integer pageSize, String  condition, String orderBy,Boolean flag, String paramJson, String queryValue) {
        JSONObject fmBaseDataOne = fmBaseService.getLatestFormByDefine(fromDefineId,pageNo,pageSize,condition,orderBy,
                flag,paramJson,queryValue);
        return ResTool.build().data(fmBaseDataOne);
    }

    /*=======================================================*/
    @ApiOperation(value = "修改业务数据(完成)", notes = "修改业务数据")
    @PostMapping({"submitBusinessFormData"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fromJson", value = "业务Json数据"),
            @ApiImplicitParam(name = "businessId", value = "业务ID"),
            @ApiImplicitParam(name = "fromDefineId", value = "表单id"),
            @ApiImplicitParam(name = "paramJson", value = "附属参数，时间，创建人等")
    })
    public ResTool submitBusinessFormData(
                                                          String fromJson, Long fromDefineId, Long businessId  ,String paramJson) {
        Map<String, Object> props = JSON.parseObject(fromJson);
        fmBaseService.submitBusinessFormData(fromJson,fromDefineId,businessId);
        return ResTool.ok;
    }

    @ApiOperation(
            value = "提交业务数据(完成)", notes = "提交业务数据")
    @PostMapping({"submitBusinessStartFormData"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fromJson", value = "业务Json数据"),
            @ApiImplicitParam(name = "fromDefineId", value = "表单id"),
            @ApiImplicitParam(name = "paramJson", value = "附属参数，时间，创建人等(暂时不用)")
    })
    public ResTool submitBusinessStartFormData(String fromJson, Long fromDefineId ,String paramJson) {
        List<String> json = new ArrayList<>();

        fmBaseService.submitBusinessStartFormData(fromJson,fromDefineId);
        return ResTool.ok;
    }

    @ApiOperation(value = "删除业务数据(完成)", notes = "删除业务数据")
    @GetMapping({"deleteData"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessIds", value = "业务id集合(逗号分隔)"),
            @ApiImplicitParam(name = "fromDefineId", value = "表单id")
    })
    public ResTool deleteData(String businessIds, Long fromDefineId) {
        String delString = fmBaseService.deleteData(businessIds, fromDefineId);
        return ResTool.build().success(true).data(delString);
    }

}
